package Test;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author 徐志荣
 * @date 2023/4/17 20:23
 */



public class StudentAlter {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test4?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection =  dataSource.getConnection();

        System.out.println("请输入要修改的列:");
        String id = scanner.next();
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生性别:");
        String sex = scanner.next();
        System.out.println("请输入学生年龄:");
        int age = scanner.nextInt();
        System.out.println("请输入学生地址:");
        String addr = scanner.next();


        String sql = "update  student set stuName = ?,stuSex = ?,stuAge = ?,stuAddr = ? where stuId = ?";

        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setString(1,name);
        statement.setString(2,sex);
        statement.setInt(3,age);
        statement.setString(4,addr);
        statement.setInt(5, Integer.parseInt(id));

        System.out.println(statement);

        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);
        statement.close();
        connection.close();

    }

}
